package com.example.demo.juc;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * ClassName: CyclicBarrierDemo
 * author: bob.ly
 * Version: 1.0.0
 * DateTime: 2021/07/31-23:13:00
 * Description:
 */
public class CyclicBarrierDemo {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7, () -> {
            System.out.println("集齐7课龙珠，召唤神龙");
        });

        /**
         * 定义七颗龙珠
         */
        for (int i = 1; i < 8; i++) {
            new Thread(() -> {
                System.out.println("第" + Thread.currentThread().getName() + "课龙珠收集到了");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }, String.valueOf(i)).start();
        }
    }
}
